<?php
/**
 * Created by PhpStorm.
 * User: gaogu
 * Date: 2018/7/4
 * Time: 16:32
 */

namespace App\Repositories;

$THRIFT_ROOT = __DIR__ . '/../../extend/thrift';
//包含thrift PHP加载器
require_once $THRIFT_ROOT . '/lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php';

use Thrift\ClassLoader\ThriftClassLoader;

//定义接口文件路径
$loader = new ThriftClassLoader();
//设置thrift命名空间
$loader->registerNamespace('Thrift', $THRIFT_ROOT . '/lib/php/lib');
//注册生成文件路径
$loader->register();

use Thrift\Protocol\TBinaryProtocol;
use Thrift\Protocol\TCompactProtocol;
use Thrift\Protocol\TMultiplexedProtocol;
use Thrift\Transport\TFramedTransport;
use Thrift\Transport\TSocket;

class ThriftRepositories
{
    CONST SEND_TIMEOUT = 500;
    CONST RECEIVE_TIMEOUT = 1000;

    CONST READ_BUFFER = 1024;
    CONST WRITE_BUFFER = 1024;

    //调用方式
    CONST PYTHON_SERVICE = 'pyhone_service';
    CONST INFORMATION_CENTER_SERVICE = 'information_center_service';

    private $server_ip;
    private $server_port;
    private $client_class;

    private $socket;
    private $protocol;
    private $transport;

    private $send_timeout;
    private $receive_timeout;

    //调用哪的服务
    private $call_to_type;

    public function __construct($client_class, $config, $send_timeout = self::SEND_TIMEOUT, $receive_timeout = self::RECEIVE_TIMEOUT, $call_to_type = self::PYTHON_SERVICE) {
        if (!is_array($config)) {
            recordLog('配置文件不存在');
            exit;
        }
        $this->client_class = $client_class;
        if (!isset($config['ip']) || empty($config['ip'])) {
            recordLog('配置文件ip不存在');
            exit;
        }
        $this->server_ip = $config['ip'];
        if (!isset($config['port']) || empty($config['port'])) {
            recordLog('配置文件port不存在');
            exit;
        }
        $this->server_port = $config['port'];
        $this->send_timeout = $send_timeout;
        $this->receive_timeout = $receive_timeout;
        $this->call_to_type = $call_to_type;

    }

    public function connection() {
        $this->socket = new TSocket($this->server_ip, $this->server_port);

        $this->setSendTimeout();
        $this->setReceiveTimeout();

        if ($this->call_to_type == self::INFORMATION_CENTER_SERVICE) {
            $this->transport = $this->socket;
            $this->protocol = new TCompactProtocol($this->transport);
        } else {
            $this->transport = new TFramedTransport($this->socket, self::READ_BUFFER, self::WRITE_BUFFER);
            $this->protocol = new TBinaryProtocol($this->transport);
        }
    }

    public function setSendTimeout() {
        $this->socket->setSendTimeout($this->send_timeout);
    }

    public function setReceiveTimeout() {
        $this->socket->setRecvTimeout($this->receive_timeout);
    }

    public function open() {
        $this->transport->open();
    }

    public function getClient() {
        $client = NULL;
        for ($i = 0; $i < 2; $i++) {
            try {
                $this->connection();
                $server_clint = '\\' . $this->client_class . '\\' . $this->client_class . 'Client';
                if ($this->call_to_type == self::INFORMATION_CENTER_SERVICE) {
                    $this->protocol = new TMultiplexedProtocol($this->protocol, 'coinServiceProcessor');
                }
                $client = new $server_clint($this->protocol);
                $this->open();
                break;
            } catch (\Exception $e) {
                recordLog($this->client_class . '链接超时，第' . ($i + 1) . '次');
                usleep(100000);
            }
        }
        return $client;
    }

    public function __destruct() {
        $this->transport->close();
    }
}